package com.yu.newsSys.servlet;

import com.yu.newsSys.constants.NewsConstants;
import com.yu.newsSys.constants.SessionConstants;
import com.yu.newsSys.domain.News;
import com.yu.newsSys.service.NewsService;
import com.yu.newsSys.service.impl.NewsServiceImpl;
import com.yu.newsSys.utils.CommonUtils;
import com.yu.newsSys.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet(name = "news",urlPatterns = "/news")
public class NewsServlet extends HttpServlet {

    private NewsService newsService = new NewsServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("getNews")) {
            this.getNews(req,resp);
        } else if (method.equals("add")) {
            this.add(req,resp);
        } else if (method.equals("getNewsById")) {
            this.getNewsById(req,resp);
        } else if (method.equals("edit")) {
            this.edit(req,resp);
        } else if (method.equals("remove")) {
            this.remove(req,resp);
        }
    }



    private void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String tempId = req.getParameter("id");
        int id = Integer.parseInt(tempId);
        CommonUtils.logInfo("remove", "NewsServlet", id);
        Result result = newsService.remove(id);
        CommonUtils.printData(result,resp);
    }

    // 编辑文章
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取并转换int类型id
        String tempId = req.getParameter("id");
        int id = Integer.parseInt(tempId);


        // 获取字符串格式的时间并以sdf解析后转为Date对象
        String date = req.getParameter("newsDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date newsDate = null;
        try {
            newsDate = new java.sql.Date(sdf.parse(date).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 获取其他属性 封装为News对象
        String newsName = req.getParameter("newsName");
        String layoutName = req.getParameter("layoutName");
        String newsAuthor = req.getParameter("newsAuthor");
        String newsTitle = req.getParameter("newsTitle");
        String newsInfo = req.getParameter("newsInfo");
        String newsContent = req.getParameter("newsContent");

        News news = new News(id,newsDate,newsName,layoutName,newsAuthor,newsTitle,newsInfo,newsContent);

        CommonUtils.logInfo("edit","servlet",news);

        Result result = newsService.edit(news);

        CommonUtils.printData(result,resp);
    }

    // 根据id查询数据
    private void getNewsById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String tempId = req.getParameter("id");
        CommonUtils.logInfo("getNewsById","servlet",tempId);
        int id = Integer.parseInt(tempId);
        CommonUtils.logInfo("getNewsById","servlet",id);
        Result result = newsService.getNewsById(id);
        CommonUtils.printData(result,resp);
    }


    protected void getNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String newsPaperName = req.getParameter("newsPaperName");
        String fileTitle = req.getParameter("fileTitle");
        String tempCurrentPage = req.getParameter("currentPage");
        // 获取当前页码，若没有赋值(页面第一次加载)则默认查询第一页
        Integer currentPage = 1;
        if (tempCurrentPage != null) {
            currentPage =Integer.parseInt(tempCurrentPage);
        }

        Result result = newsService.getNews(newsPaperName,fileTitle,currentPage, NewsConstants.pageSize);

        CommonUtils.printData(result,resp);
    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取字符串格式的时间并以sdf解析后转为Date对象
        String date = req.getParameter("newsDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.sql. Date newsDate = null;
        try {
            newsDate = new java.sql.Date(sdf.parse(date).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 获取其他属性 封装为News对象
        String newsName = req.getParameter("newsName");
        String layoutName = req.getParameter("layoutName");
        String newsAuthor = req.getParameter("newsAuthor");
        String newsTitle = req.getParameter("newsTitle");
        String newsInfo = req.getParameter("newsInfo");
        String newsContent = req.getParameter("newsContent");
        News news = new News(newsDate, newsName, layoutName, newsAuthor, newsTitle, newsInfo, newsContent);

        CommonUtils.logInfo("add","servlet",news);
        // 调用service层的add方法返回result
        Result result = newsService.add(news);
        CommonUtils.printData(result,resp);
    }
}
